package zyf.jmidifile.test;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.LinkedList;

import zyf.jmidifile.JMidiEvent;

public class TrackAdapter extends RecyclerView.Adapter<TrackAdapter.VH> {
    private LinkedList<JMidiEvent> events = new LinkedList<>();
    private static  final int VIEW_EVENTS_LIMIT = 20;

    public TrackAdapter() {
    }

    public void add(@NonNull JMidiEvent e) {
        if (e.data == null) return;
        if (events.size() == VIEW_EVENTS_LIMIT) {
            events.removeLast();
        }
        events.addFirst(e);
        notifyDataSetChanged();
    }

    public void clear() {
        events.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, int position) {
        holder.setEvent(events.get(position));
    }

    @Override
    public int getItemCount() {
        return events.size();
    }

    public static class VH extends RecyclerView.ViewHolder {
        public final TextView titleLbl;
        public final TextView nameLbl;
        public boolean state = true;

        public VH(View v) {
            super(v);
            titleLbl = (TextView) v.findViewById(R.id.tv_title);
            nameLbl = (TextView) v.findViewById(R.id.tv_cmd_name);
        }

        public void setEvent(final JMidiEvent e) {
            // 设置名字
            int pos = getLayoutPosition();
            String title = String.format("[%s]", MainActivity.toMinSecStr(e.ms));
            for (int i = 0; i < e.data.length; ++i) {
                title += String.format(" %02x", e.data[i]);
            }
            titleLbl.setText(title);
            nameLbl.setText(getName(e));
        }

        private static String getName(final JMidiEvent e) {
            int type = e.getEventType();
            if (type == JMidiEvent.TYPE_META) {
                return "meta";
            }

            if (type == JMidiEvent.TYPE_SYSEX0 || type == JMidiEvent.TYPE_SYSEX1) {
                return "sysex";
            }

            // common event
            switch (type) {
                case JMidiEvent.TYPE_NOTE_ON :
                case JMidiEvent.TYPE_NOTE_OFF :
                    return String.format("note %d %s", e.getPitch(), e.getVelocity() > 0 ? "on" : "off");
                case JMidiEvent.TYPE_AFTER_TOUCH :
                    return "颤音";
                case JMidiEvent.TYPE_CONTROLLER :
                    return "CC";
                case JMidiEvent.TYPE_TIMBRE :
                    return "音色配置";
                case JMidiEvent.TYPE_PRESSURE :
                    return "通道压力";
                case JMidiEvent.TYPE_PITCH_BEND :
                    return "弯音轮";
                default:
                    return "未知";
            }
        }
    }
}

